﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class NetUnionFightChariot : NetUnionFightBase
    {
        public Dictionary<long, InstUFCariot> chariots = new Dictionary<long, InstUFCariot>();

        public NetUnionFightChariot(RoomUnionFight unionFight, RoomCharactorManager mgr, RoomNetUnionFight net)
            :base(unionFight,mgr,net)
        {
        }


        public override void Enter()
        {
            base.Enter();
            chariots.Clear();
        }

        public override void Tick()
        {
            base.Tick();
        }

        public override void Leave()
        {
            base.Leave();
            chariots.Clear();
        }

        public override RoomCharactor CreateCharactor(long uuid)
        {
            var chariot = chariots[uuid];
            charactorManager.CreateUnionChariot(chariot);
            chariots[uuid] = chariot;
            return GetCharactor(uuid);
        }

        public override void ClearCharactorData(long uuid)
        {
            base.ClearCharactorData(uuid);

            if (chariots.ContainsKey(uuid))
                chariots.Remove(uuid);
        }

        /// <summary>
        /// 刷新战车
        /// </summary>
        /// <param name="msg"></param>
        public void ChariotRefresh(SCUFCaropt msg)
        {
            if (msg != null)
            {

                for(int i=0;i<msg.cariot.Count;++i)
                {
                    var chariot = msg.cariot[i];
                    var uuid = chariot.guid;
                    var pos = msg.local[i];
                    ChariotRefresh(chariot);
                    postions[uuid] = new Vector3(pos.x, pos.y, pos.z);
                    if (!angles.ContainsKey(uuid))
                        angles[uuid] = 0f;
                    var charactor = GetCharactor(uuid);
                    var move = charactor.move as RCMoveChariot;
                    move.PlaceTo(postions[uuid], angles[uuid]);
                    UpdatePosition(uuid, new Vector3(pos.x, pos.y, pos.z));

                    var ai = charactor.ai as RCAIChariot;
                    ai.SetHpEffect((float)(chariot.hp / chariot.maxHp));
                }
            }
        }

        /// <summary>
        /// 刷新战车
        /// </summary>
        /// <param name="inst"></param>
        public void ChariotRefresh(InstUFCariot inst)
        {
            var uuid = inst.guid;

            var instFactory = net.netChariotFactory.chariotFartorys[inst.factoryGuid];
            
            chariots[uuid] = inst;
            states[uuid] = (int)UnionChariotState.Stand;
            
            var charactor = GetCharactor(uuid,false);
            if (charactor == null)
            {
                charactor = CreateCharactor(uuid);
            }
            
            angles[uuid] = 0f;
            UpdateState(uuid, (int)UnionChariotState.Stand);

            var ai = charactor.ai as RCAIChariot;
            ai.SetHpEffect( (float)(inst.hp / inst.maxHp) );
        }

        /// <summary>
        /// 战车死亡
        /// </summary>
        /// <param name="msg"></param>
        public void ChariotDead(SCUFCariotCrash msg)
        {
            var uuid =  msg.guid;
            UpdateState(uuid, (int)UnionChariotState.Death);
        }

        /// <summary>
        /// 战车打门
        /// </summary>
        /// <param name="msg"></param>
        public void ChariotAttackGate(SCUFCariotAttackGate msg)
        {
            var uuid =  msg.guid;
            UpdateState(uuid, (int)UnionChariotState.Attack);
            var charactor = GetCharactor(uuid);
            var ai = charactor.ai as RCAIChariot;
            ai.PlayAttack();
        }
     
    }
}